package com.zz.yt.test.click;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.landicorp.android.eptapi.DeviceService;
import com.landicorp.android.eptapi.device.Printer;

import com.blankj.utilcode.util.ToastUtils;
import com.landicorp.android.eptapi.exception.ReloginException;
import com.landicorp.android.eptapi.exception.RequestException;
import com.landicorp.android.eptapi.exception.ServiceOccupiedException;
import com.landicorp.android.eptapi.exception.UnsupportMultiProcess;
import com.zz.yt.lib.bluetooth.delegate.printer.AssistPrinterStep;
import com.zz.yt.lib.bluetooth.delegate.printer.PrintLister;
import com.zz.yt.lib.bluetooth.delegate.printer.PrinterContent;
import com.zz.yt.test.R;

public class PrinterActivity extends Activity implements PrintLister {

    private AssistPrinterStep step;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_printer);
    }

    public void onClick(View view) {

        PrinterContent printerContent = new PrinterContent();
        if (null == step) {
            step = new AssistPrinterStep(printerContent);
            step.setLister(this);
        }

        int ret = step.getPrinterStatus();
        if (ret != Printer.ERROR_NONE) {
            return;
        }
        step.init(getApplicationContext());
        step.addContent();
        step.startStepPrint();
    }


    @Override
    public void success() {
        ToastUtils.showShort("打印成功");
    }

    @Override
    public void filed() {
        ToastUtils.showShort("打印失败");
    }

    @Override
    public void onResume() {
        super.onResume();
        bindDeviceService();
    }

    @Override
    public void onPause() {
        super.onPause();
        unbindDeviceService();
    }

    public void bindDeviceService() {
        try {
            DeviceService.login(getApplicationContext());
        } catch (RequestException | ServiceOccupiedException | ReloginException | UnsupportMultiProcess e) {
            e.printStackTrace();
        }
    }


    public void unbindDeviceService() {
        DeviceService.logout();
    }
}
